﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twipler.App.DataDoc;
using Xunit;
using Xunit.Extensions;

namespace UnitTests.App.DataDoc
{
	public class DataDoc
	{
		[Fact]
		public void WithSingleItemFindIt()
		{
			List<DataDocValue> item = new List<DataDocValue> {new DataDocValue("test", 1)};
			IDataDoc doc = new DataDocList(item);

			DataDocValue result = doc.Find("test");

			Assert.NotNull(result);
			Assert.Equal(1, result.Value);
		}

		[Fact]
		public void SelectForTwoItemsReturnsTwoItems()
		{
			List<DataDocValue> sublist = new List<DataDocValue> { new DataDocValue("one", 1), new DataDocValue("two", 2) };
			List<DataDocValue> list = new List<DataDocValue> { new DataDocValue("", sublist) };

			IDataDoc doc = new DataDocList(list);

			List<IDataDoc> result = doc.Select("/");

			Assert.NotNull(result);
			Assert.Equal(2, result.Count());
		}


		[Fact]
		public void SelectForTwoItemsWhichAreListsReturnsSame()
		{
			List<DataDocValue> sublist = new List<DataDocValue>
			                             	{
			                             		new DataDocValue("0", new List<DataDocValue>()
			                             		                      	{
			                             		                      		new DataDocValue("one", 1),
			                             		                      		new DataDocValue("two", 2)
			                             		                      	}
			                             			),

												new DataDocValue("1", new List<DataDocValue>()
			                             		                      	{
			                             		                      		new DataDocValue("three", 3),
			                             		                      		new DataDocValue("four", 4)
			                             		                      	}
			                             			)
			                             	};

			List<DataDocValue> list = new List<DataDocValue> { new DataDocValue("", sublist) };

			IDataDoc doc = new DataDocList(list);

			List<IDataDoc> result = doc.Select("/");

			Assert.NotNull(result);
			Assert.Equal(2, result.Count());

			// can find first child and its value is 1
			Assert.NotNull(result[0].Find("one"));
			Assert.Equal(1, result[0].Find("one").Value);

		}

		[Fact]
		public void CanSelectFirstItemFromTree()
		{
			List<DataDocValue> sublist = new List<DataDocValue>
			                             	{
			                             		new DataDocValue("0", new List<DataDocValue>()
			                             		                      	{
			                             		                      		new DataDocValue("one", 1),
			                             		                      		new DataDocValue("two", 2)
			                             		                      	}
			                             			),

												new DataDocValue("1", new List<DataDocValue>()
			                             		                      	{
			                             		                      		new DataDocValue("three", 3),
			                             		                      		new DataDocValue("four", 4)
			                             		                      	}
			                             			)
			                             	};

			List<DataDocValue> list = new List<DataDocValue> { new DataDocValue("", sublist) };

			IDataDoc doc = new DataDocList(list);

			List<IDataDoc> result = doc.Select("/0");

			Assert.NotNull(result);
			Assert.Equal(2, result.Count());
		}

	}
}
